//
//  LoginController.m
//  敢唆嘛!
//
//  Created by lanou on 15/11/23.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "LoginController.h"
#import "Device.h"
#import "RegisterController.h"
#import "Define.h"
#import "FogetPassWordController.h"
#import "UserProtocolController.h"
#import "SettingController.h"
@interface LoginController ()

@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建界面标题
    self.navigationItem.title = @"登陆";
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建左上角返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, KSWidth / 18, KSWidth / 18);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-unie61e"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBackBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftBackBtn;
    
    NSArray *labelArray = @[@"用户名",@"密码"];
    
    for (int i= 0; i<2; i++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 40+i*50, KSWidth/5, 30)];
        label.text = labelArray[i];
        [self.view addSubview:label];
        
    }
    
    //用户名输入框
    self.registerTextField = [[UITextField alloc]initWithFrame:CGRectMake(50+KSWidth/5, 40, KSWidth/2, 30)];
    self.registerTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.registerTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
   
//    [_registerTextField.layer setCornerRadius:5];
//    
//    _registerTextField.layer.borderWidth = 3;
//    
//    _registerTextField.layer.borderColor = [UIColor grayColor].CGColor;
    
    [self.view addSubview:_registerTextField];
    
    
    //密码输入框
    self.passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(50+KSWidth/5, 90, KSWidth/2, 30)];
    
    self.passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
//    [_passwordTextField.layer setCornerRadius:5];
//    
//    _passwordTextField.layer.borderWidth = 3;
//    
//    _passwordTextField.layer.borderColor = [UIColor grayColor].CGColor;
    
    _passwordTextField.secureTextEntry = YES;
    
    [self.view addSubview: _passwordTextField];
    
    //登陆button 注册 忘记密码
    NSArray *titleArray = @[@"登陆" , @"注册", @"忘记密码"];
    
    for (int i = 0; i<3; i++) {
        
        UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
        
        registerButton.frame = CGRectMake((KSWidth-280)/2+100*i, KSHeight/3, 80, 30);
        registerButton.layer.borderColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0].CGColor;
        
        [registerButton.layer setCornerRadius:10];
        
        registerButton.layer.borderWidth = 1;
        
        [registerButton setTitle:titleArray[i] forState:UIControlStateNormal];
        
        [registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        registerButton.tag = 1000+i*100;
        
        [self.view addSubview:registerButton];
    }
    

//     //第三方登陆显示的label
//    UILabel * thirdLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KSWidth, 30)];
//    
//    thirdLabel.center = CGPointMake(KSWidth/2, KSHeight/2);
//    
//    thirdLabel.text = @"支持第三方登陆";
//    
//    thirdLabel.textAlignment = NSTextAlignmentCenter;
//    
//    [self.view addSubview:thirdLabel];
    
//    //循环创建三个第三方登陆按钮 新浪微博登陆 qq账号登陆 微信账号登陆
//    NSArray *imageArray = @[@"qq.png", @"weixin.png", @"xinlang.png"];
//    for (int i = 0; i<3; i++) {
//        UIButton *ThirdButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        
//        ThirdButton.frame = CGRectMake(20+100*i, KSHeight/5*3, 100, 100);
//        
//        [ThirdButton setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
//        
//        [ThirdButton addTarget:self action:@selector(thirdButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//        
//        ThirdButton.tag = 2000+i*100;
//        
//        [self.view addSubview:ThirdButton];
//    }
    
    //注释协议的label
    
    UILabel *annotationLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, KSWidth, 20)];
    
    annotationLabel.center = CGPointMake(KSWidth/2, KSHeight-80);
    
    annotationLabel.textAlignment = NSTextAlignmentCenter;
    
    annotationLabel.font = [UIFont systemFontOfSize:12];
    
    annotationLabel.text = @"注册登陆即表示您同意我们的用户许可协议";
    
    annotationLabel.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userProtocol)];
    
    [annotationLabel addGestureRecognizer:tap];
    
    [self.view addSubview:annotationLabel];
    
}

// 返回按钮触发
- (void)backBtnAction:(UIButton *)backBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark --登陆 注册 忘记密码 按钮触发的事件 ---
-(void)registerButtonAction:(UIButton *)btn{
    //判断是否有网络 如果没有网络
    if (![[NetWorkState shareInstance]reachability]) {
        
        [self alertActionWithTitle:@"提示" message:@"网络无连接" other:@"确定"];
        
        return;
    }

    
    //登陆
    if (btn.tag == 1000) {
        
        //bmob的登陆方法
       [BmobUser loginWithUsernameInBackground:_registerTextField.text password:_passwordTextField.text block:^(BmobUser *user, NSError *error) {
           //如果用户和密码有不正确的 有提示
           if (error) {
                [self alertActionWithTitle:@"提示" message:@"用户名或密码错误" other:@"确定"];
           }
           
           if (user) {
               
               BmobQuery *bquery = [BmobQuery queryWithClassName:@"_User"];
               [bquery getObjectInBackgroundWithId:user.objectId block:^(BmobObject *object, NSError *error) {
                   if (object) {
                       BmobFile *file = [object objectForKey:@"userHeader"];
                       UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
                       
                       if (file == nil) {
                    imageView.image = [UIImage imageNamed:@"user.png"];
                }
                        else{
                    [imageView sd_setImageWithURL:[NSURL URLWithString:file.url]];
                    NSLog(@"%@",imageView.image);
                }
                           
                    //判断block实现 是否存在 如果不存在 相当于为nil 不进条件进行回调;
                    if (self.block != nil) {
                                              
                    self.block(_registerTextField.text,imageView);
            }

                       
        }
                   
        }];
//               //用户头像设置
//               BmobObject *object = [BmobObject objectWithoutDatatWithClassName:@"_User" objectId:user.objectId];
//               BmobFile *file = [object objectForKey:@"userHeader"];
//               
//               UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
//               
//               if (file == nil) {
//                   imageView.image = [UIImage imageNamed:@"user.png"];
//               }
//               else{
//                   [imageView sd_setImageWithURL:[NSURL URLWithString:file.url]];
//                   NSLog(@"%@",imageView.image);
//               }
//               
//               //判断block实现 是否存在 如果不存在 相当于为nil 不进条件进行回调;
//               if (self.block != nil) {
//                   
//                   self.block(_registerTextField.text,imageView);
//               }
               //返回用户主界面
               
               [self.navigationController popViewControllerAnimated:YES];
               
           }
    }];
        
        
        
    }
    //注册
    else if (btn.tag == 1100){
        //初始化注册界面
        RegisterController *registerVC = [[RegisterController alloc]init];
        
        [self.navigationController pushViewController:registerVC animated:YES];
    }
    //忘记密码
    else{
        
        FogetPassWordController *fogetVC = [[FogetPassWordController alloc]init];
        
        [self.navigationController pushViewController:fogetVC animated:YES];
    }
    
}


#pragma mark--第三方登陆 点击的触发的事件 --
-(void)thirdButtonAction:(UIButton *)btn{
    //判断是否有网络 如果没有网络
    if (![[NetWorkState shareInstance]reachability]) {
        
        [self alertActionWithTitle:@"提示" message:@"网络无连接" other:@"确定"];
        
        return;
    }

    
    //点击的是qq
    if (btn.tag == 2000) {
        
    }
    //点击微信
    else if (btn.tag == 2100){
        
        
    }
    //点击新浪微博
    else{
        
        
    }
}

#pragma mark --用户协议触发手势的事件--
-(void)userProtocol{
    
    UserProtocolController *userProtocolVC = [[UserProtocolController alloc]init];
    
    [self.navigationController pushViewController:userProtocolVC animated:YES];
}
//提示页面
- (UIAlertController *)alertActionWithTitle:(NSString *)title message:(NSString *)message other:(NSString *)other
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:nil];
    [alertVc addAction:otherAction];
    [self presentViewController:alertVc animated:YES completion:nil];
    return alertVc;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark--点击textField外 键盘弹回 --
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.registerTextField resignFirstResponder];
    
    [self.passwordTextField resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
